//
//  NSOperationDemo1ViewController.m
//  多线程编程
//
//  Created by GR on 2017/7/15.
//  Copyright © 2017年 GR Harry. All rights reserved.
//

#import "NSOperationDemo1ViewController.h"
#import <UIImageView+WebCache.h>
#import <SDWebImageManager.h>
#import "ConcurrentOperation.h"

@interface NSOperationDemo1ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UILabel *label;

@property (strong, nonatomic) NSURLSession *session;

@property (weak, nonatomic) NSBlockOperation *downloadOpertion1;
@end

@implementation NSOperationDemo1ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        [self startDownloadImage];
    });
}

- (void)startDownloadImage {
    self.label.text = @"正在下载图片";
    
    __block UIImage *image1 = nil;
    __block UIImage *image2 = nil;
    
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    
    NSBlockOperation *downloadOpertion1 = [NSBlockOperation blockOperationWithBlock:^{
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/879873.jpg"]];
        image1 = [UIImage imageWithData:imageData];
    }];
    
//    self.downloadOpertion1 = downloadOpertion1;
    
    NSBlockOperation *downloadOpertion2 = [NSBlockOperation blockOperationWithBlock:^{
        NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://tupian.enterdesk.com/2012/0506/1/11.jpg"]];
        image2 = [UIImage imageWithData:imageData];
    }];
    
    /*
     自定义operation执行。
    ConcurrentOperation *downloadOpertion1 = [[ConcurrentOperation alloc] initWithOperationBlock:^ (OperationCompletion completion) {
        [SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:@"http://imgstore.cdn.sogou.com/app/a/100540002/879873.jpg"] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            image1 = image;
            
            completion();
        }];
    }];
    ConcurrentOperation *downloadOpertion2 = [[ConcurrentOperation alloc] initWithOperationBlock:^ (OperationCompletion completion) {
        [SDWebImageManager.sharedManager downloadImageWithURL:[NSURL URLWithString:@"http://tupian.enterdesk.com/2012/0506/1/11.jpg"] options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            image2 = image;

            completion();
        }];
    }];
    */
    [queue addOperations:@[downloadOpertion1, downloadOpertion2] waitUntilFinished:YES];
    
    // 在上面阻塞了当前线程，所以到达这里的时候就是任务已全部完成。
    CGSize size = self.imageView.bounds.size;
    UIGraphicsBeginImageContext(size);
    
    [image1 drawInRect:CGRectMake(0, 0, size.width, size.height / 2.f)];
    [image2 drawInRect:CGRectMake(0, size.height / 2.f, size.width, size.height / 2.f)];
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        self.imageView.image = newImage;
        self.label.text = @"图片合成完成";
    }];
}


@end
